package com.gxa.ssm251.controller;

import com.gxa.ssm251.exception.BusinessException;
import com.gxa.ssm251.pojo.Admin;
import com.gxa.ssm251.pojo.City;
import com.gxa.ssm251.service.AdminService;
import com.gxa.ssm251.util.JsonResult;
import com.gxa.ssm251.util.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

//@Controller("cpController")
//@Controller
@RestController
@RequestMapping("/SMS")
@Api(value = "执行验证码的控制器")
@CrossOrigin //跨域的注解
public class SMSController {

//    @Autowired
//    @Resource
//    @Qualifier("cpService")
//    private CpService cpService;

    @Resource
    private AdminService adminService;

    @ApiOperation(value = "判断邮箱信息并发送信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "admin", value = "json格式的管理员对象", dataType = "json")
    )
    @ResponseBody
    @PostMapping("/checkCode")
    public JsonResult checkCode(@RequestBody Admin admin, HttpSession session) {
        JsonResult jsonResult = new JsonResult();
        String email = admin.getEmail();
        Admin smsadmin = adminService.selectOnlyadmin(email);
        if (smsadmin != null) {
            String code = sendEamilCode(admin.getEmail());
            session.setAttribute("email", email);
            session.setAttribute("vcode", code);
            System.out.println("code=" + code);
            jsonResult.setMsg("验证码发送成功");
            jsonResult.setCode("1");
        } else {
            jsonResult.setMsg("验证码发送失败,该邮箱没有注册");
            jsonResult.setCode("0");
        }
        return jsonResult;
    }

    @ApiOperation(value = "判断邮箱信息并发送信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "admin", value = "json格式的管理员对象", dataType = "json")
    )
    @ResponseBody
    @PostMapping("/updatepwd")
    public JsonResult updatePwd(@RequestBody Map map, HttpSession session) {
        String mapEmail = map.get("email").toString();
        String mapPwd = map.get("pwd").toString();
        String mapVcode = map.get("vcode").toString();
        //下面五条输出是用来检查参数的
        System.out.println("********************");
        System.out.println(mapEmail);
        System.out.println(mapPwd);
        System.out.println(mapVcode);
        System.out.println(session.getAttribute("email"));
        System.out.println(session.getAttribute("vcode"));
        System.out.println("********************");

        JsonResult jsonResult = new JsonResult();
        if (session.getAttribute("email").equals(mapEmail)) {
            if (session.getAttribute("vcode").equals(map.get("vcode").toString())) {
                String uuid = UUID.randomUUID().toString().
                        replace("-", "").substring(0, 8);
                mapPwd = MD5Util.encode(mapPwd.toString() + uuid);
                Integer lines = adminService.xiugai(mapEmail, mapPwd, uuid);
                if (lines > 0) {
                    jsonResult.setMsg("修改成功!");
                    jsonResult.setCode("1");
                } else {
                    jsonResult.setMsg("未知原因,修改失败!");
                    jsonResult.setCode("0");
                }
            } else {
                jsonResult.setMsg("验证码错误,请重新输入");
                jsonResult.setCode("0");
            }
        } else {
            jsonResult.setMsg("该邮箱地址已更改,请检查邮箱");
            jsonResult.setCode("0");
        }
        return jsonResult;
    }

    //发送验证码的方法
    public String sendEamilCode(String email) {
        Random random = new Random();
        String vcode = (random.nextInt(10000)) + "";//生成随机4位的验证码
        System.out.println("验证码 = " + vcode);

        HtmlEmail send = new HtmlEmail();
        String resultCode = vcode;
        try {
            send.setHostName("smtp.qq.com");//发送服务
            send.setSmtpPort(465);//发送端口号
            send.setSSLOnConnect(true);//开启 SSL 加密
            send.setCharset("utf-8");
            send.addTo(email);
            send.setFrom("227573171@qq.com");
            send.setAuthentication("227573171@qq.com", "itjuuikrnftgbied");
            send.setSubject("呆鸡");
            send.setMsg("hello，呆鸡！验证码是：" + resultCode + "，请谨慎输入");
            send.send(); //发送
            /**
             *  send.addTo(“这里面放的接收人的QQ邮箱”);
             *  send.setSubject(“标题”);
             *  send.setMsg(“内容”);
             send.setFrom(“这里面放的是发送人的QQ邮箱”);
             send.setAuthentication(“这里放的是发送人QQ邮箱”,”这个是QQ授权码”);
             */
        } catch (EmailException e) {
            e.printStackTrace();
        }
        return resultCode;//返回验证码
    }

}
